package org.ricks.common.db;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description:
 * @date 2020/12/2114:19
 */
public class ColumnListHandler<T> extends AbstractListHandler<T> {
    private final int columnIndex;
    private final String columnName;

    public ColumnListHandler() {
        this(1, (String)null);
    }

    public ColumnListHandler(int columnIndex) {
        this(columnIndex, (String)null);
    }

    public ColumnListHandler(String columnName) {
        this(1, columnName);
    }

    private ColumnListHandler(int columnIndex, String columnName) {
        this.columnIndex = columnIndex;
        this.columnName = columnName;
    }

    protected T handleRow(ResultSet rs) throws SQLException {
        return (T) (this.columnName == null ? rs.getObject(this.columnIndex) : rs.getObject(this.columnName));
    }
}